import { IRequest } from './type'
import { UserController, TeacherController } from './controller'

const fnDebugLog = require('debug')('main')

class Module {
  public controllersList: any[] = [ ]
  constructor (ctrlClassList: any[]) {
    ctrlClassList.forEach((ctrlClass)=> {
      this.controllersList.push(new ctrlClass())
    })
  }
}
function initial() {
  return new Module([UserController, TeacherController])
}

const requestsList: IRequest[] = [
  { method: 'get', route: 'users',      option: { head: { token: '124', app: 'admin' }, params: { 'age'    : '>10' }}},
  { method: 'get', route: 'users/vip',  option: { head: { token: '456', app: 'shop'  }, params: { 'name'   : 'mike'}}},
  { method: 'get', route: 'teachers',   option: { head: { token: '789', },              params: { 'subject': 'cs'}}},
]
function main () {
  const m = initial();
  const controllers = m.controllersList;
  requestsList.forEach((req) => {
    fnDebugLog('+++++++ request: ', req);
    for (let controller of controllers) {
      fnDebugLog('...', controller, controller.root)
      switch(req.method) {
        case 'get':
          controller.handlersOfGet?.forEach((handler:any) => {
            const subRoute = !handler.route ? controller.root : `${controller.root}/${handler.route}`
            if (subRoute !== req.route) return;
            const result = controller[handler.method](req.option)
            fnDebugLog('result', result)
          });
          break;
      }
      
    }
    fnDebugLog('------ request ------')
  })
}

main()
